package com.mcl.mydinglib;

import com.mcl.ijetplugin.Constants;
import com.mcl.ijetplugin.MCLPlugin;
import com.mcl.ijetplugin.MclCommunication;
import com.mcl.ijetplugin.ParseAdapter;
import com.mcl.ijetplugin.ResultMsgCode;
import com.mcl.ijetplugin.beans.receiver.BaseResultBean;
import com.mcl.ijetplugin.beans.receiver.DynamicResultBean;
import com.mcl.ijetplugin.beans.receiver.HeartBeatResultBean;
import com.mcl.ijetplugin.beans.sender.BlockContent;
import com.mcl.ijetplugin.beans.sender.DynamicSendBlockBean;
import com.mcl.ijetplugin.beans.sender.HeartBeatSendBean;
import com.mcl.ijetplugin.exception.MCLEncryptionException;
import com.mcl.ijetplugin.inter.OnParseListerner;

import java.net.SocketException;
import java.util.Scanner;

public class MyClass {

    public static void main(String[] args) {
        try {
            mainScannerLogic();
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }


    /**
     * @throws SocketException
     */
    private static void mainScannerLogic() throws SocketException {
        //show current plugin version
        System.out.println(MCLPlugin.VERSION_CODE);

        //new the udp communication object that have two params. one:printer IP,two:port
        MclCommunication mclCommunication = new MclCommunication("192.168.13.22", 61104);
        mclCommunication.setPrintHex(true);

        //set communication callback
        mclCommunication.setParseAdapter(new ParseAdapter(new OnParseListerner() {
            @Override
            public void parseResult(BaseResultBean messageBean) {
                if (Constants.PROTOCOL_25 == messageBean.getProtocol()) {//heatbeat callback for the mac address.
                    HeartBeatResultBean heartBeatResultBean = (HeartBeatResultBean) messageBean;
                    processResultRSGC(heartBeatResultBean);
                } else if (Constants.PROTOCOL_26 == messageBean.getProtocol()) {//dynamic update  callback for text content
                    DynamicResultBean dynamicResultBean = (DynamicResultBean) messageBean;
                    processResultRSGC(dynamicResultBean);
                }
            }
        }));

        //connect
        mclCommunication.connect();

        try {
            boolean isLoop = true;
            while (isLoop) {
                Scanner scanner = new Scanner(System.in);
                String msg = scanner.next();
                if ("h".equals(msg)) {
                    HeartBeatSendBean heartBeatComBean = new HeartBeatSendBean(true);
                    mclCommunication.send(heartBeatComBean);
                } else if ("d".equals(msg)) {
                    DynamicSendBlockBean dynamicSendBlockBean = new DynamicSendBlockBean();

                    BlockContent blockContent1 = new BlockContent();
                    blockContent1.setContentBuffer("abc");
                    blockContent1.setBlockIndex(1);
                    blockContent1.setBlockType(1);
                    dynamicSendBlockBean.addBlockContentList(blockContent1);

//            BlockContent blockContent2 = new BlockContent();
//            blockContent2.setContentBuffer("def");
//            blockContent2.setBlockIndex(2);
//            blockContent2.setBlockType(4);
//            dynamicSendBlockBean.addBlockContentList(blockContent2);

                    mclCommunication.send(dynamicSendBlockBean);
                    //System.out.println(dynamicSendBlockBean.getDatas().length);

                } else if ("exit".equals(msg)) {
                    isLoop = false;
                }
            }
        } catch (MCLEncryptionException e) {//The Software will be throw this exception if you before don't register. First all you need execute mclLicence-1.0 to register it.
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            mclCommunication.close();
            System.out.println("over");
        }


    }

    /**
     * parse result
     *
     * @param baseResultBean
     */
    private static void processResultRSGC(BaseResultBean baseResultBean) {
        if (baseResultBean != null) {
            switch (baseResultBean.getMsgTranslateCode()) {
                case ResultMsgCode.RSGC_SUCCESS:
                    //success
                    break;
                case ResultMsgCode.RSGC_FAIL:
                    //fail
                    break;
            }
        }
    }

}